﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.BusinessLogic.Interfaces;
using KCMS.BusinessLogic.Models;
using KCMS.DataAcsess.Interfaces;
using StructureMap;
using AutoMapper;

namespace KCMS.BusinessLogic.Engine
{
    public class MenuService:IMenuService
    {
        private IMenuManager menuManager = ObjectFactory.GetInstance<IMenuManager>();
        public Menu getById(Guid id)
        {
            DataAcsess.Entitys.MenuEntity menuEntity = menuManager.getById(id);
            return Mapper.Map<DataAcsess.Entitys.MenuEntity, Models.Menu>(menuEntity);
        }

        public Menu getByCode(string code)
        {
            
            var menuEntity = (from t in menuManager.getAll()
                              where (t.Code.Equals(code))
                              select t).SingleOrDefault();
            Menu menu = Mapper.Map<DataAcsess.Entitys.MenuEntity, Models.Menu>(menuEntity);

            IMenuItemService menuItemService = ObjectFactory.GetInstance<IMenuItemService>();
            List<MenuItem> itemList= menuItemService.getByMenuId(menuEntity.Id).ToList<MenuItem>();
           
            menu.Items = itemList;
           
            return menu;
        }

        public IEnumerable<Menu> getAll()
        {
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.MenuEntity>, IEnumerable<Models.Menu>>(menuManager.getAll());
        }



        public void update(Menu menu)
        {
            menuManager.update(Mapper.Map<Models.Menu,DataAcsess.Entitys.MenuEntity>(menu));
        }

        public void delete(Guid id)
        {
            menuManager.delete(id);
        }

        public void save(Menu menu)
        {
            menuManager.save(Mapper.Map<Models.Menu, DataAcsess.Entitys.MenuEntity>(menu));
        }
    }
}
